package cn.shiro.shiroservice.config;

import cn.shiro.shiroservice.common.utils.FastJson2JsonRedisSerializerUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;


@Configuration
public class RedisConfig {

    @Bean("redisTemplateMy")
    public RedisTemplate<String, Object> setRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 设置 key 的序列化规则
        template.setKeySerializer(new StringRedisSerializer());
        // 设置 value 的序列化规则
        template.setValueSerializer(new FastJson2JsonRedisSerializerUtils<>(Object.class));
        // 设置 hash key 的序列化规则
        template.setHashKeySerializer(new FastJson2JsonRedisSerializerUtils<>(Object.class));
        // 设置 hash value 的序列化规则
        template.setHashValueSerializer(new FastJson2JsonRedisSerializerUtils<>(Object.class));
        template.afterPropertiesSet();
        return template;
    }

}